﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Advanced_SendCallback_Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void leetSocket1_OnConnectedEVENT()
        {
            //invoke is required because the socket system works in a separate thread. 
            //Therefore invoking this method in gui thread with the help of the method invoke,
            //allows us to manipulate gui objects, see for further explanation msdn, or google
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(delegate() { leetSocket1_OnConnectedEVENT(); }));
                return;
            }

            label2.Text = "Connected: true";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] a = new byte[1024*1024*((int)numericUpDown1.Value)];
            progressBar1.Value = 0;
            leetSocket1.sendObject(a, false, true, false);            
        }

        private void leetSocket1_OnSendChunkEVENT(byte[] CustomHeader, ulong TotalLength, int SentBytes, ulong SentBytesAddition, bool isFinished, int CommandID)
        {    
            //invoke is required because the socket system works in a separate thread. 
            //Therefore invoking this method in gui thread with the help of the method invoke,
            //allows us to manipulate gui objects, see for further explanation msdn, or google
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MethodInvoker(delegate() { leetSocket1_OnSendChunkEVENT(CustomHeader, TotalLength,  SentBytes,  SentBytesAddition,  isFinished,  CommandID); }));
                return;
            }
            
            progressBar1.Value = (int)((SentBytesAddition * 100) / TotalLength);
            if (isFinished)
            {
                MessageBox.Show("Client: Sending finished.");
            }
        }
    }
}
